# -*- coding: utf-8 -*-
import scrapy
from weibo_scrapy.settings import LIMIT_DATE,COOKIES_ENABLED,COOKIES
import re
import json
class AwbSpider(scrapy.Spider):
    name = 'awb'
    allowed_domains = ['m.weibo.cn','media.weibo.cn','card.weibo.com']
    # start_urls = ['https://m.weibo.cn/api/container/getIndex?containerid=231018{uid}_-_longbloglist_original'.format(uid=uid)]
    def start_requests(self):
        with open('ids.txt', 'r') as fp:
            uids = [uid.strip() for uid in fp]
        uids = ['2665144']
        for uid in uids:
            url = 'https://m.weibo.cn/api/container/getIndex?containerid=231018{uid}_-_longbloglist_original'.format(uid=uid)
            yield scrapy.Request(url=url, cookies=COOKIES)
    def parse(self, response):
        # print(response.text)
        urls = re.findall('"page_url":"(.*?)"',response.text)
        for url in urls:
            url = url.replace('\\','')
            # print(url)
            # 微博文章新版页面和旧版页面
            if 'object_id' in url:
                yield scrapy.Request(url=url,cookies=COOKIES,callback=self.parse_new)
            elif 'containerid' in url:
                cid = re.findall('containerid=(.*?)&',url)[0]
                url = 'http://card.weibo.com/article/aj/articleshow?cid={cid}&vid=5447625353&extparam=&from=1110006030&wm=0&ip=10.77.40.36&version=3&lang=zh-cn&query=&_=1547448853646'.format(cid=cid)
                yield scrapy.Request(url=url,callback=self.parse_old)
        next_page = re.findall('"page":(\d+)',response.text)
        # print(next_page)
        if next_page:
            origin_url = response.url
            if 'page' not in origin_url:
                new_url = origin_url + '&page=' + next_page[0]
            else:
                new_url = re.sub('page=\d+','page=' + next_page[0],origin_url)
            # print(new_url)
            yield scrapy.Request(url=new_url,callback=self.parse)
    def parse_new(self,response):
        data = json.loads(re.findall(r'var \$render_data = \[(.*?)\]\[0\]', response.text, re.S)[0])
        print(data['title'])
        return

    def parse_old(self,response):
        r_data = json.loads(response.text)
        msg = r_data['msg']
        code = r_data['code']
        if msg == 'ok' and code == '100000':
            data = r_data['data']
            title = data['title']
            print(title)
        return